package com.rub.android.jetpack

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.Navigation
import com.rub.android.R
import kotlinx.android.synthetic.main.fragment_home.*

private const val ARG_USER_NAME = "user_name"
private const val ARG_AGE = "age"

class HomeFragment : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        button.setOnClickListener {
            val navController = Navigation.findNavController(it)

            //val args = newArgs("参数1", "参数2")
            //home to detail
            navController.navigate(R.id.action_homeFragment_to_detailFragment, newInstance())
        }
    }


    /**
     * 学习博客：https://blog.csdn.net/Scxioi0/article/details/108918235
     */
    companion object {

        /**
         * 方法1，指定参数名称与类型，进行传参数与值
         */
        @JvmStatic
        fun newArgs(username: String, age: String) =
            Bundle().apply {
                putString(ARG_USER_NAME, username)
                putString(ARG_AGE, age)
            }

        /**
         * 方法2，使用safe-args插件，指定名称与类型，进行传参数与值
         * 还可以进行指定对象，执行序列号传值(Serializable不推荐,Parcelable推荐)
         */
        @JvmStatic
        fun newInstance():Bundle{
            return HomeFragmentArgs.Builder()
                .setAge(20)
                .setUserName("蹭蹭")
                .build().toBundle()
        }

    }
}